<template>
  <div id="app" style="overflow: hidden">
    <!--   搜索 searchBar-->
    <div style="height: calc(100vh - 200px)">
      <div
          class="flex"
          style="
          justify-content: space-between;
          position: relative;
          align-items: center;
          flex-wrap: wrap;
        "
      >
        <div class="flex" style="margin-right: 20px">
          <div class="search">
            司机编码：
            <el-input
                clearable
                placeholder="请输入"
                style="width: 200px"
                size="small"
                prefix-icon="el-icon-search"
                v-model="params.driverCode"
            >
            </el-input>
          </div>
          <div class="search">
            司机姓名：
            <el-input
                clearable
                placeholder="请输入"
                style="width: 200px"
                size="small"
                prefix-icon="el-icon-search"
                v-model="params.driverName"
            >
            </el-input>
          </div>
          <div class="search">
            常跑省级区域：
            <el-select
                style="width: 200px"
                v-model="params.province"
                multiple
                size="small"
                placeholder="请选择"
            >
              <el-option
                  v-for="item in provinceData"
                  :key="item.code"
                  :label="item.name"
                  :value="item.name"
              >
              </el-option>
            </el-select>
            <!-- <el-cascader
              size="small"
              v-model="selectedAddress"
              :options="addressList"
              @change="getAddressData"
              :props="{
                checkStrictly: true,
                label: 'name',
                value: 'code',
                children: 'children',
              }"
              clearable
              filterable
              placeholder="请选择省市区"
              style="width: 200px"
            /> -->
          </div>
          <div class="search">
            司机电话：
            <el-input
                clearable
                placeholder="请输入"
                style="width: 200px"
                size="small"
                prefix-icon="el-icon-search"
                v-model="params.driverPhone"
            >
            </el-input>
          </div>
          <div class="search">
            车牌号：
            <el-input
                clearable
                placeholder="请输入"
                style="width: 200px"
                size="small"
                prefix-icon="el-icon-search"
                v-model="params.carNumber"
            >
            </el-input>
          </div>
          <div class="search">
            车型名称：
            <el-select
                style="width: 200px"
                v-model="params.carTypeName"
                size="small"
                placeholder="请选择"
            >
              <el-option
                  v-for="item in carTypeCodeData"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
              >
              </el-option>
            </el-select>
            <!-- <el-select
              v-model="params.storeType"
              placeholder="请选择"
              style="width: 200px"
              size="small"
              clearable
            >
              <el-option label="加盟店" value="F"></el-option>
              <el-option label="直营店" value="R"></el-option>
            </el-select> -->
          </div>
          <div class="search">
            <el-button
                type="primary"
                size="small"
                @click="_search"
                icon="el-icon-search"
            >查询
            </el-button>
            <el-button
                type="primary"
                size="small"
                @click="_reset"
                icon="el-icon-refresh"
            >重置
            </el-button>
          </div>
          <!--    新增按钮 -->
          <!--        <div style="position: absolute;right:0;bottom: 10px">-->
          <!--          <el-button style="background-color: #303e9d;color: #eeeeee" size="small" @click="addStore"-->
          <!--                     v-if="authorityList.includes(301)">新增店铺-->
          <!--          </el-button>-->
          <!--        </div>-->
        </div>
      </div>
      <!-- <div style="margin-bottom: 5px">
        <el-button
          style="background-color: #303e9d; color: #eeeeee"
          size="small"
          @click="addStore"
          v-if="authorityList.includes(301)"
          ><i class="el-icon-plus" style="margin-right: 5px"></i>新 增
        </el-button>
      </div> -->
      <!--    tableBar-->
      <el-table
          :data="storeList"
          height="calc(100vh - 280px)"
          overflow="auto"
          border
          stripe
          :cell-style="{ textAlign: 'center' }"
          :header-cell-style="{
          fontSize: '14px',
          'background-color': '#F3F6FD',
          color: '#333333',
          textAlign: 'center',
        }"
      >
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column
            prop="driverCode"
            label="司机编码"
            width="200px"
            show-overflow-tooltip
        >
          <template slot-scope="scope">
            <p
                @click="showDriverOrder(scope.row.driverCode)"
                style="text-decoration: underline; cursor: pointer"
            >
              {{ scope.row.driverCode }}
            </p>
          </template>
        </el-table-column>
        <el-table-column
            prop="driverName"
            width="100px"
            label="司机姓名"
        ></el-table-column>
        <el-table-column
            prop="driverPhone"
            label="司机电话"
            width="120px"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="carNumber"
            label="车牌号"
            width="250px"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="carType"
            label="车牌类型"
            width="100px"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="driverAverageMark"
            label="司机平均分"
            width="100px"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="carTypeName"
            label="车型名称"
            width="250px"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="province"
            label="常跑省级区域"
            show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag
                style="margin-right: 4px"
                v-for="(item, index) in scope.row.province"
                :key="index"
                size="mini"
            >{{ item }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="orderCount"
            label="订单数量"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
            show-overflow-tooltip
        ></el-table-column>
        <!-- <el-table-column label="门店类型">
          <template slot-scope="scope">
            <span v-if="scope.row.storeType == 'F'">加盟店</span>
            <span v-if="scope.row.storeType == 'R'">直营店</span>
          </template>
        </el-table-column> -->

        <!-- <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-link
              style="color: #303e9d; margin-left: 10px"
              @click="editStore(scope.row)"
              v-if="authorityList.includes(302)"
              >编辑
            </el-link>
            <el-link
              style="color: #303e9d; margin-left: 10px"
              @click="deleteStore(scope.row)"
              v-if="authorityList.includes(304)"
              >删除
            </el-link>
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <!-- 分页 -->
    <div style="margin-top: 20px; text-align: right">
      <el-pagination
          align="center"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNumber"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
      >
      </el-pagination>
    </div>

    <el-dialog :visible.sync="dialogVisible" title="相关订单" width="895px">
      <el-table
          :data="driverOderList"
          overflow="auto"
          border
          stripe
          :cell-style="{ textAlign: 'center' }"
          :header-cell-style="{
          fontSize: '14px',
          'background-color': '#F3F6FD',
          color: '#333333',
          textAlign: 'center',
        }"
      >
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column prop="driverName" w label="司机姓名"></el-table-column>
        <el-table-column
            prop="driverGetAmount"
            label="司机佣金"
        ></el-table-column>
        <el-table-column prop="driverMark" label="司机评分"></el-table-column>
        <el-table-column
            prop="userPayAmount"
            label="付款金额"
        ></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import dayjs from 'dayjs'
import addressList from '../../assets/js/address.js'

export default {
  name: 'driverList',
  meta: {
    title: '司机列表',
    icon: 'el-icon-truck',
    authorityList: 300,
    sort: 6,
  },
  data() {
    return {
      driverOderList: [],
      dialogVisible: false,
      createTime: '',
      maxHeight: '',
      storeDialog: false,
      detailsDialog: false,
      dialogTitle: '',
      storeList: [],
      areaList: [],
      cascaderProps: {
        value: 'code',
        label: 'name',
        children: 'children',
        emitPath: true,
      },
      addressList: addressList,
      selectedAddress: '',
      authorityList: [],
      totalCount: 0,
      storeDetails: {},
      storeForm: {
        storeName: '',
        address: '',
        areaId: '',
        addUserName: '',
        addUserPhone: '',
        storeType: 1,
      },
      params: {
        pageSize: 20,
        driverCode: '',
        driverName: '',
        pageNumber: 1,
        driverPhone: '',
        carNumber: '',
        carTypeName: '',
        province: '', //行政区域
      },

      provinceData: [],
      carTypeCodeData: [],
    }
  },
  computed: {
    ...mapState(['userData']),
  },
  mounted() {
    this.$nextTick(() => {
      this.maxHeight = window.innerHeight - 350
    })
    window.onresize = () => {
      this.$nextTick(() => {
        this.maxHeight = window.innerHeight - 350
      })
    }
  },
  created() {
    this.getStoreList()
    this.getSelectValData()
    // this.getAreaLis t();
    const {authorityList = []} =
    this.$quinn.getStorage('userDatav3') || this.$store.state.userData || {}
    this.authorityList = authorityList
  },
  methods: {
    showDriverOrder(driverCode) {
      this.$api.driverGarageOrderRecord({driverCode}).then((res) => {
        console.log('查看', res)

        if (res && res.length) {
          this.driverOderList = []
          this.driverOderList = res
          this.dialogVisible = true
        } else {
          this.$message.info('暂无更多数据')
        }
      })
    },
    getSelectValData() {
      this.$api.driverGarageProvincialEnum().then((res) => {
        this.provinceData = res
      })
      this.$api.driverGarageCarTypeEnum().then((res) => {
        this.carTypeCodeData = res
      })
    },
    _search() {
      this.params.pageNumber = 1
      this.getStoreList()
    },
    getAddressData(val) {
      if (Array.isArray(val) && val.length > 0) {
        this.params.parentCode = val[0] || ''
        this.params.areaCode = val[1] || ''
      } else {
        this.params.parentCode = ''
        this.params.areaCode = ''
      }
    },
    getStoreList() {
      this.$api.driverGarage(this.params).then((res) => {
        console.log('司机列表', res)

        this.storeList = res.data || []
        this.totalCount = res.total || 0
      })
    },
    _reset() {
      this.params = this.$options.data.call(this).params
      this.getStoreList()
    },
    getAreaList() {
      // this.$api.assignGetAreaList().then(res => {
      //   this.areaList = res || [];
      // });
    },
    getDate(val) {
      if (val) {
        this.params.startTime = dayjs(val[0]).format('YYYY-MM-DD')
        this.params.endTime = dayjs(val[1]).format('YYYY-MM-DD')
      } else {
        this.params.startTime = ''
        this.params.endTime = ''
      }
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getStoreList()
    },
    handleCurrentChange(val) {
      this.params.pageNumber = val
      this.getStoreList()
    },
    addStore() {
      this.$refs.addStore.handleOpen()
    },
    editStore(row) {
      this.dialogTitle = '编辑店铺'
      this.storeForm = JSON.parse(JSON.stringify(row))
      this.$refs.addStore.handleOpen(this.storeForm)
    },
    viewDetails(row) {
      this.storeDetails = row
      this.detailsDialog = true
    },
    deleteStore(row) {
      this.$confirm('确定要删除该店铺吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
          .then(() => {
            this.$api.storeDelStoreById({storeId: row.storeId}).then((res) => {
              this.$message.success('删除成功')
              this.getStoreList()
            })
          })
          .catch(() => {
          })
    },
    receiveMessage(val) {
      if (val) {
        this.getStoreList()
      }
    },
  },
}
</script>

<style scoped>
@import '../../assets/css/public.css';

.search {
  margin: 0 10px 10px 0;
  display: flex;
  align-items: center;
}

.flex {
  display: flex;
  flex-wrap: wrap;
}

/deep/ .el-form-item__error {
  left: 80px;
}

/deep/ .el-dialog__title {
  color: #fff;
}

/deep/ .el-dialog {
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

/deep/ .el-dialog__header {
  background-color: #303e9d;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}
</style>
